﻿using Inaction;
using Inaction.ServiceContract;
using Inaction.Core;
using Inaction.Mapping;
using Inaction.Security;
using Inaction.ServiceProxy;
using System;
using System.Security.Principal;

namespace Inaction.Host
{
    static class HostHelper
    {
        private static LocalProxy localProxy = new LocalProxy();

        #region 服务端执行(调用LocalProxy执行)
        public static Response Login(Request request)
        {
            try
            {
                var requestData = request.RequestData;
                Inaction.Security.Principal.Instance = new DefaultPrincipal(requestData.Identity);
                var result = ServiceContainer.GetService<ISecurityProvider>().Login();
                return new Response(new ResponseData(result));
            }
            catch (Exception e)
            {
                return new Response(new ResponseData(e));
            }
        }
        public static Response Query(Request request)
        {
            try
            {
                var requestData = request.RequestData;
                Inaction.Security.Principal.Instance = new DefaultPrincipal(requestData.Identity);
                var sql = requestData.Data as SqlStatement;
                var result = localProxy.Query(requestData.EntityType, sql);
                return new Response(new ResponseData(result));
            }
            catch (Exception e)
            {
                return new Response(new ResponseData(e));
            }
        }
        public static Response Save(Request request)
        {
            try
            {
                var requestData = request.RequestData;
                Inaction.Security.Principal.Instance = new DefaultPrincipal(requestData.Identity);
                var obj = requestData.Data as ISavable;
                var result = localProxy.Save(obj);
                return new Response(new ResponseData(result));
            }
            catch (Exception e)
            {
                return new Response(new ResponseData(e));
            }
        }
        public static Response Excute(Request request)
        {
            try
            {
                var requestData = request.RequestData;
                Inaction.Security.Principal.Instance = new DefaultPrincipal(requestData.Identity);
                var obj = requestData.Data as SqlCommand;
                var result = localProxy.Excute(obj);
                return new Response(new ResponseData(result));
            }
            catch (Exception e)
            {
                return new Response(new ResponseData(e));
            }
        }
        public static Response Invoke(Request request)
        {
            try
            {
                var requestData = request.RequestData;
                Inaction.Security.Principal.Instance = new DefaultPrincipal(requestData.Identity);
                var obj = requestData.Data as RemotingMethod;
                var result = localProxy.Invoke(obj);
                return new Response(new ResponseData(result));
            }
            catch (Exception e)
            {
                return new Response(new ResponseData(e));
            }
        }
        #endregion
    }
}